<template>
  <Modal v-model="modal.show" width="350" :title="modal.title">
    <Form ref="form" :model="formItem" :rules="ruleValidate" :label-width="100">
      <FormItem label="ICD-10编码" prop="code1" label-width="120"><Input v-model="formItem.code1" placeholder=""
          style="width: 200px" />
      </FormItem>
      <FormItem label="ICD-10名称" prop="name1" label-width="120"><Input v-model="formItem.name1" placeholder=""
          style="width: 200px" />
      </FormItem>
      <FormItem label="医保ICD编码" prop="code2" label-width="120"><Input v-model="formItem.code2" placeholder=""
          style="width: 200px" />
      </FormItem>
      <FormItem label="医保ICD名称" prop="name2" label-width="120"><Input v-model="formItem.name2" placeholder=""
          style="width: 200px" />
      </FormItem>
      <FormItem label="有效性" prop="code3" label-width="120"><Input v-model="formItem.code3" placeholder=""
          style="width: 200px" />
      </FormItem>
    </Form>
    <template #footer>
      <div>
        <Button type="text" @click="modal.show = false">取消</Button>
        <Button type="primary" @click="handleOk">确定</Button>
      </div>
    </template>
  </Modal>
</template>
<script setup>
import http from '@/util/http'
import _ from 'lodash'
let { proxy } = getCurrentInstance()
const emit = defineEmits(['initList'])
let modal = reactive({ show: false, title: '' })
const form = ref()

let formItem = reactive({
  group: '主诊断',
  code1: '',
  name1: '',
  code2: '',
  name2: '',
  code3: '',
})

const show = (type, data) => {
  form.value.resetFields()
  modal.show = true
  formItem.spid = ''
  if (type == 1) {
    modal.title = '添加主诊断'
  } else {
    var clone = _.cloneDeep(data)
    formItem = clone
    modal.title = '编辑主诊断'
  }
}

const ruleValidate = {
  code1: [{ required: true, message: '请输入', trigger: 'blur' }],
  code2: [{ required: true, message: '请输入', trigger: 'blur' }],
  code3: [{ required: true, message: '请输入', trigger: 'blur' }],
  name1: [{ required: true, message: '请输入', trigger: 'blur' }],
  name2: [{ required: true, message: '请输入', trigger: 'blur' }],
}

const handleOk = () => {
  form.value.validate((valid) => {
    if (valid) {
      http.post('/Basic/PolicyEnum/AddOrUpdate', toRaw(formItem), (response) => {
        if (response.data.flag) {
          emit('initList')
          proxy.$Message.success(response.data.msg)
          modal.show = false
        } else {
          proxy.$Message.error(response.data.msg)
        }
      })
    } else {
      return false
    }
  })
}

defineExpose({
  show,
})
</script>
